// ==UserScript==
// @name         GB学院V1.0
// @namespace    http://tampermonkey.net/
// @version      2024-10-10
// @description  try to take over the world!
// @author       ElyarEnwer
// @match        https://www.xjgbzx.cn/pc/index.html
// @icon         https://www.google.com/s2/favicons?sz=64&domain=xjgbzx.cn
// @grant        none


// ==/UserScript==

(function () {
    'use strict';
    wait_until('div.content_left > div > ul', target => {
        addElement(target[0]);
        addElementXuanXiu(target[0]);
    });

})();

function wait_until(selector, callback) {
    var timer = setInterval(() => {
        let target = get_element_length(selector);
        if (target.length > 0) {
            clearInterval(timer);
            console.log("目标加载完毕");
            callback(target);
        }
    }, 50);
};

function get_element_length(selector) {
    // 等待指定元素加载完毕
    return document.querySelectorAll(selector);
}


//创建UI
function addElement(container) {
    // 创建一个新的div元素
    var li = document.createElement('li');
    li.className = "item";
    li.style.fontFamily = "Microsoft YaHei";
    li.style.listStyleType = "None";
    li.style.margin = 0;
    li.style.width = "100%";
    li.style.padding = "14.5px 0";
    li.style.boxSizing = "border-box";
    li.style.marginBottom = "1px";
    li.style.background = "#E60020";
    li.style.cursor = "pointer";
    li.style.display = "flex";
    li.style.alignItems = "flex-start";
    var btn = document.createElement('div');
    btn.style.padding = "0";
    btn.style.boxSizing = "border-box";
    btn.style.width = "100px";
    btn.style.height = "46px";
    btn.style.background = "#E60020";
    btn.style.textAlign = "center";
    btn.style.fontSize = "15px";
    btn.style.color = "white";
    btn.style.lineHeight = "46px";
    btn.textContent = "一键学习[必修]";
    var inputDiv = document.createElement('div');
    inputDiv.style.color = "#333";
    inputDiv.style.padding = "0";
    inputDiv.style.margin = "0";
    inputDiv.style.boxSizing = "border-box";
    inputDiv.style.display = "flex";
    inputDiv.style.justifyContent = "center"; /* 横向居中 */
    inputDiv.style.alignItems = "center";
    inputDiv.style.width = "60px";
    inputDiv.style.position = "relative";
    inputDiv.style.fontSize = "15px";
    var input = document.createElement('input');
    input.id = "course_length";
    input.style.margin = "0";
    input.style.fontSize = "inherit";
    input.style.webkitAppearance = "none";
    input.style.backgroundColor = "#fff";
    input.style.border = "1px solid #DCDFE6";
    input.style.boxSizing = "border-box";
    input.style.display = "inline-block";
    input.style.height = "40px";
    input.style.lineHeight = "40px";
    input.style.padding = "0 15px";
    input.style.textAlign = "center";
    input.style.transition = "border-color .2s cubic-bezier(.645,.045,.355,1)";
    input.style.width = "50px";
    input.style.outline = "none";
    input.style.color = "#666";
    input.style.borderRadius = "40px";
    inputDiv.appendChild(input);

    var span = document.createElement('span');
    span.textContent = "节课";
    span.style.height = "46px";
    span.style.display = "inline-block";
    span.style.background = "#E60020";
    span.style.textAlign = "center";
    span.style.marginLeft = "15px";
    span.style.fontSize = "16px";
    span.style.color = "white";
    span.style.lineHeight = "46px";
    span.style.boxSizing = "border-box";
    li.appendChild(btn);
    li.appendChild(inputDiv);
    li.appendChild(span);
    container.appendChild(li);

    //点击事件
    btn.addEventListener('click', async e => {
        let dataArray = await getCourseData();
        let to_study_count = parseInt(document.getElementById("course_length").value);
        if (to_study_count > dataArray.length) {
            alert("课时数超标了！")
        } else {
            //自动学习
            var i = 0;
            for (i; i < to_study_count; i++) {
                let payload = await getPayload(dataArray[i].id);
                await study(payload);
            }
            alert("学习完毕!");
        }
    }, true);

}
//创建UI
function addElementXuanXiu(container) {
    // 创建一个新的div元素
    var li = document.createElement('li');
    li.className = "item";
    li.style.fontFamily = "Microsoft YaHei";
    li.style.listStyleType = "None";
    li.style.margin = 0;
    li.style.width = "100%";
    li.style.padding = "14.5px 0";
    li.style.boxSizing = "border-box";
    li.style.marginBottom = "1px";
    li.style.background = "#E60020";
    li.style.cursor = "pointer";
    li.style.display = "flex";
    li.style.alignItems = "flex-start";
    var btn = document.createElement('div');
    btn.style.padding = "0";
    btn.style.boxSizing = "border-box";
    btn.style.width = "100px";
    btn.style.height = "46px";
    btn.style.background = "#E60020";
    btn.style.textAlign = "center";
    btn.style.fontSize = "15px";
    btn.style.color = "white";
    btn.style.lineHeight = "46px";
    btn.textContent = "一键学习[选修]";
    var inputDiv = document.createElement('div');
    inputDiv.style.color = "#333";
    inputDiv.style.padding = "0";
    inputDiv.style.margin = "0";
    inputDiv.style.boxSizing = "border-box";
    inputDiv.style.display = "flex";
    inputDiv.style.justifyContent = "center"; /* 横向居中 */
    inputDiv.style.alignItems = "center";
    inputDiv.style.width = "60px";
    inputDiv.style.position = "relative";
    inputDiv.style.fontSize = "15px";
    var input = document.createElement('input');
    input.id = "course_length_xuan";
    input.style.margin = "0";
    input.style.fontSize = "inherit";
    input.style.webkitAppearance = "none";
    input.style.backgroundColor = "#fff";
    input.style.border = "1px solid #DCDFE6";
    input.style.boxSizing = "border-box";
    input.style.display = "inline-block";
    input.style.height = "40px";
    input.style.lineHeight = "40px";
    input.style.padding = "0 15px";
    input.style.textAlign = "center";
    input.style.transition = "border-color .2s cubic-bezier(.645,.045,.355,1)";
    input.style.width = "50px";
    input.style.outline = "none";
    input.style.color = "#666";
    input.style.borderRadius = "40px";
    inputDiv.appendChild(input);

    var span = document.createElement('span');
    span.textContent = "节课";
    span.style.height = "46px";
    span.style.display = "inline-block";
    span.style.background = "#E60020";
    span.style.textAlign = "center";
    span.style.marginLeft = "15px";
    span.style.fontSize = "16px";
    span.style.color = "white";
    span.style.lineHeight = "46px";
    span.style.boxSizing = "border-box";
    li.appendChild(btn);
    li.appendChild(inputDiv);
    li.appendChild(span);
    container.appendChild(li);

    //点击事件
    btn.addEventListener('click', async e => {
        let dataArray = await getCourseDataXuan();
        let to_study_count = parseInt(document.getElementById("course_length_xuan").value);
        if (to_study_count > dataArray.length) {
            alert("课时数超标了！")
        } else {
            //自动学习
            var i = 0;
            for (i; i < to_study_count; i++) {
                let payload = await getPayload(dataArray[i].id);
                await study(payload);
            }
            alert("学习完毕!");
        }
    }, true);

}



//获取必学课程信息
async function getCourseData() {
    let url = "https://www.xjgbzx.cn/trainee/api/course/class_course/217";
    return await fetch(url).then(res => {
        return res.json();
    }).then(data => {
        return data.data.course[0].uncompleted_courseData[0].courses;
    });
}

//获取选学课程信息
async function getCourseDataXuan() {
    return await fetch("https://www.xjgbzx.cn/trainee/api/course/uncompleted?currentPage=1&pageSize=100&year=2024", {
        "headers": {
            "accept": "application/json, text/plain, */*",
            "accept-language": "zh-CN,zh;q=0.9",
            "cache-control": "no-cache",
            "content-type": "application/json",
            "sec-ch-ua": "\"Not:A-Brand\";v=\"99\", \"Chromium\";v=\"112\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"macOS\"",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "signature": "adfasfsdaffsdafsdafaj"
        },
        "referrer": "https://www.xjgbzx.cn/pc/index.html",
        "referrerPolicy": "origin-when-cross-origin",
        "body": "{}",
        "method": "POST",
        "mode": "cors",
        "credentials": "include"
    }).then(res => {
        return res.json();
    }).then(data => {
        return data.data.courses;
    });
}


//获取payload
async function getPayload(courseID) {
    let payload = {};
    let playUrl = `https://www.xjgbzx.cn/trainee/api/course/play/${courseID}`;
    await fetch(playUrl).then(res => {
        return res.json();
    }).then(data => {
        payload.playCourse = data.data.playCourse;
    });

    let url = `https://www.xjgbzx.cn/trainee/api/course/detail/${courseID}`
    return await fetch(url).then(res => {
        return res.json();
    }).then(courseData => {
        let info = courseData.data.course;
        payload.user_course_id = info.user_course_id;
        payload.scormData = [{
            "sco_id": JSON.parse(info.manifest)[0].sco_id,
            "lesson_location": "10000",
            "session_time": 10000
        }];
        return payload;
    });
}

async function study(payload) {
    await fetch("https://www.xjgbzx.cn/trainee/index/user_course", {
        "headers": {
            "accept": "application/json, text/plain, */*",
            "accept-language": "zh-CN,zh;q=0.9",
            "cache-control": "no-cache",
            "content-type": "application/json",
            "sec-ch-ua": "\"Not:A-Brand\";v=\"99\", \"Chromium\";v=\"112\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"macOS\"",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "signature": "adfasfsdaffsdafsdafaj"
        },
        "referrer": "https://www.xjgbzx.cn/pc/index.html",
        "referrerPolicy": "origin-when-cross-origin",
        "body": JSON.stringify(payload),
        "method": "POST",
        "mode": "cors",
        "credentials": "include"
    }).then(res => {
        return res.json();
    }).then(data => {
        if (data.code === 0) {
            console.log("学习完毕：", payload.user_course_id);
        } else {
            console.log("学习失败：", payload.user_course_id);
        }

    });

}
